Skip to content

BUG: Fixed encoding of pd.NA with to_json #31748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

ArtificialQualia
Copy link
Contributor

@@ -25,8 +25,9 @@ Fixed regressions
Bug fixes
~~~~~~~~~

-
-
**I/O**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ArtificialQualia

This should be

I/O
^^^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarcoGorelli I've updated that to the correct format.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original was actually fine, to be consistent with the v1.0.1 file

(sorry for the conflicting comments)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake, sorry, I'd taken that from v1.1.0 where we go back to

I/O
^^^

, will be more careful in the future

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarcoGorelli no problem! It's also inconsistent between the different files .. (for the bug fix releases, the list of bugs is much smaller, so less worth it to make subsections. At least, that is what we just did for the v1.0.1 file)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted to original format

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great start. Can you also run benchmarks to ensure no perf regressions?

@@ -1671,3 +1671,15 @@ def test_to_s3(self, s3_resource):
assert target_file in (
obj.key for obj in s3_resource.Bucket("pandas-test").objects.all()
)

@pytest.mark.parametrize(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the comment around parametrization got lost in GH comments but in any case would rather parametrize on nulls_fixture + pd.NA if we don't think we can add pd.NA to the former for now

I don't think the current parametrization is super useful, so OK to just take the first case and put it in the body of the test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parametrize on nulls_fixture + pd.NA

+1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't find a way to add pd.NA to a pytest fixture without recreating the contents of the fixture entirely, so I've added them as separate tests. Let me know if you were thinking of something else.

Note that pd.NaT is the only object that doesn't use its string representation when encoded from a label.

@ArtificialQualia
Copy link
Contributor Author

@WillAyd I've ran the full asv suite, and then re-ran any asv tests that had a ratio >1.1

There are no significant changes from this PR.

Let me know if you want any more information or results files.

@WillAyd WillAyd added IO JSON read_json, to_json, json_normalize Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate labels Feb 7, 2020
Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WillAyd I've ran the full asv suite, and then re-ran any asv tests that had a ratio >1.1

I think I understand though ideally shouldn't have gotten anything over 1.1 in the first place. Did you run the entire suite or just asv continuous upstream/master HEAD -b io.json.ToJSON?

The latter is all you needed here, so noting for future reference.

Otherwise small nit / question for @simonjayhawkins . Can get this in soon

result = pd.DataFrame({pd.NA: ["a"]}).to_json()
assert result == '{"<NA>":{"0":"a"}}'

def test_json_pandas_nulls(self, nulls_fixture):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simonjayhawkins do you know of an easy way to add pd.NA to nulls_fixture inline during injection? Or would adding another fixture on top be the only way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we already have two nulls_fixtures (nulls_fixture and unique_nulls_fixture), I think adding another could be the simplest way for now.

I think, in general, simpler to skip than to add a extra parameter (although I've not kept up with the latest pytest releases).

There is not a huge number of tests using the nulls_fixture and maybe tests that do should also test for pandas.NA, either now of in the future. So an alternative could be to add pd.NA to the existing nulls fixture and either skip of xfail.

result = pd.DataFrame([[pd.NA]]).to_json()
assert result == '{"0":{"0":null}}'

def test_json_pandas_na_label(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ArtificialQualia can just remove the label tests for now; not touched by your test and I don't think these expectations are necessarily correct, so let's leave that to a different exercise

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed the label tests. Let me know what the final decision is above about the fixtures.

@ArtificialQualia
Copy link
Contributor Author

@WillAyd I ran the entire suite. Whenever I run the entire suite I always get false positives, so I rerun anything that shows up on the first go-around.

There are no significant performance differences in io.json.ToJSON (or anywhere else).

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@jorisvandenbossche jorisvandenbossche added this to the 1.0.2 milestone Feb 8, 2020
@jorisvandenbossche jorisvandenbossche merged commit 0bfb8cb into pandas-dev:master Feb 8, 2020
@jorisvandenbossche
Copy link
Member

@ArtificialQualia Thanks a lot!

@lumberbot-app
Copy link

lumberbot-app bot commented Feb 8, 2020

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
$ git checkout 1.0.x
$ git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
$ git cherry-pick -m1 0bfb8cbdb7bac29d158a868817f606145bf72f06
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
$ git commit -am 'Backport PR #31748: BUG: Fixed encoding of pd.NA with to_json'
  1. Push to a named branch :
git push YOURFORK 1.0.x:auto-backport-of-pr-31748-on-1.0.x
  1. Create a PR against branch 1.0.x, I would have named this PR:

"Backport PR #31748 on branch 1.0.x"

And apply the correct labels and milestones.

Congratulation you did some good work ! Hopefully your backport PR will be tested by the continuous integration and merged soon!

If these instruction are inaccurate, feel free to suggest an improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO JSON read_json, to_json, json_normalize Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pd.NA is converted to {} instead of null with pd.DataFrame.to_json
5 participants